package com.study.experiment05.servlet;

import com.study.experiment05.bean.User;
import com.study.experiment05.bean.UserList;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

/**
 * @author WS
 */
@WebServlet(name = "UserListServlet", urlPatterns = "/experiment05/userList")
public class UserListServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html");
        response.setCharacterEncoding("UTF-8");
        PrintWriter out = response.getWriter();

        UserList checker = new UserList();
        List<User> users = checker.list();
        if (users != null) {
            out.println("<table>\n" +
                    "    <tr><th>UserName</th><th>Password</th></tr>");
            for (User user : users) {
                out.println("<tr><td>" + user.getUsername() + "</td><td>" + user.getPassword() + "</td></tr>");
            }
            out.println("</table>");
        } else {
            out.println("<p>暂无数据</p>");
        }
        out.flush();
        out.close();

    }
}
